package action.contract;

import java.util.List;

import model.TContract;
import model.THtdelay;
import model.company.Companybasic;
import model.company.Companycontact;
import model.contract.Loangrant;
import model.dropdown.DCommon;
import model.personal.Personalbasic;
import core.action.BaseAction;
import exception.Exceptions;
import service.contract.ContractService;
import service.contract.ShowContractService;

@SuppressWarnings("serial")
public class ShowContractAction extends BaseAction{
	private ShowContractService showContractService;
	private ContractService contractService;
	private List<?> htList;
	private List<?> contractList;
	private List<THtdelay> htdelay;
	private List<?> ensureList;
	private List<?> mortgageList;
	private List<?> pledgeList;
	private List<Loangrant> loangrantList;
	private String E="0";
	private String M="0";
	private String P="0";//E,M,P表示从合同是否存在
	private String page_show;
	private Integer id;//接收传过来的值
	private THtdelay delay;
	
	private String clientname;
	private String customcode;
	private String codetype;
	private String address;
	private String phone;
	private String modeofrepayment;
	private Double count=0.00;
	private Double ungrant;
	private TContract contract;
	
	private String option;
	private String title;
	
	public String execute(){
		contractList=showContractService.show_htList(" where contractstate<2");
		option="all";
		title="有效合同查询";
		return "success";
	}
	public String showfalse(){
		contractList=showContractService.show_htList(" where contractstate=2");
		option="allfalse";
		title="作废合同查询";
		return "success";
	}
	
	//审核
	public String showcheck(){
		contractList=showContractService.show_htList(" where contractstate=10");
		option="check";
		title="合同审核";
		return "success";
	}
	
	public String check() throws Exceptions{
		contract=showContractService.get_ht(id);
		if(contract.getClienttype().equals("2")){
			Companybasic company=contractService.get_companybasic(contract.getClientid());
			clientname=company.getName();
			List<DCommon> codetypelist=contractService.getIdtypeList();
			for(DCommon type:codetypelist){
				if(type.getId().equals(company.getRegcertificates())){
					codetype=type.getName();
				}
			}
			customcode=company.getRegcertificatesnumber().toString();
		}else if(contract.getClienttype().equals("1")){
			Personalbasic personal=contractService.get_personalbasic(contract.getClientid());
			clientname=personal.getName();
			List<DCommon> codetypelist=contractService.getIdtypeList();
			for(DCommon type:codetypelist){
				if(type.getId().equals(personal.getCertificatetype())){
					codetype=type.getName();
				}
			}
			customcode=personal.getCertificatenumber();
		}
		return "check";
	}
	
	public String showdelay(){
		find("ischild=1");
		option="delay";
		return "success";
	}
	
	private void find(String str){
		htList=showContractService.get_htList(str);
		//		page_show=showContractService.show_htPage(str);
	}
	
	//详情
	public String detail() throws Exceptions{
		contract=showContractService.get_ht(id);
		if(contract.getClienttype().equals("2")){
			Companybasic company=contractService.get_companybasic(contract.getClientid());
			clientname=company.getName();
			List<DCommon> codetypelist=contractService.getIdtypeList();
			for(DCommon type:codetypelist){
				if(type.getId().equals(company.getRegcertificates())){
					codetype=type.getName();
				}
			}
			customcode=company.getRegcertificatesnumber().toString();
			address=company.getRegaddress();
			Companycontact contact=contractService.get_companycontact(contract.getClientid());
			phone=contact.getTelephone();
		}else if(contract.getClienttype().equals("1")){
			Personalbasic personal=contractService.get_personalbasic(contract.getClientid());
			clientname=personal.getName();
			List<DCommon> codetypelist=contractService.getIdtypeList();
			for(DCommon type:codetypelist){
				if(type.getId().equals(personal.getCertificatetype())){
					codetype=type.getName();
				}
			}
			customcode=personal.getCertificatenumber();
			address=personal.getCurrentresidentialaddress();
			phone=personal.getHomephone();
		}
		if(contract.getModeofrepayment().equals("1")){
			modeofrepayment="一次性偿还";
		}else if(contract.getModeofrepayment().equals("2")){
			modeofrepayment="分期偿还";
		}
		else if(contract.getModeofrepayment().equals("3")){
			modeofrepayment="按约还息一次性还本";
		}else if(contract.getModeofrepayment().equals("4")){
			modeofrepayment="利随本清";
		}else if(contract.getModeofrepayment().equals("5")){
			modeofrepayment="等额本金";
		}else if(contract.getModeofrepayment().equals("6")){
			modeofrepayment="等额本息";
		}else{
			modeofrepayment="其他";
		}
	
		ensureList=showContractService.get_htRelation("contractloanid", contract.getContractId(),"ensure");
		mortgageList=showContractService.get_htRelation("contractloanid", contract.getContractId(),"mortgage");
		pledgeList=showContractService.get_htRelation("contractloanid", contract.getContractId(),"pledge");
		loangrantList=showContractService.get_loangrant("contractid="+id+" and state<100");
		if(loangrantList==null){
			loangrantList=showContractService.get_loangrant("contractid="+id+" and state=100");
		}
		for(Loangrant loangrant:loangrantList){
			if(loangrant.getState()==2){
				count+=loangrant.getGrantmoney();
			}
		}
		ungrant=contract.getApplymoney()-count;
		if(ensureList!=null) E="1";
		if(mortgageList!=null) M="1";
		if(pledgeList!=null) P="1";
		return "detail";
	}

	
	//--------GET/SET----------//
	public ShowContractService getShowContractService() {
		return showContractService;
	}

	public void setShowContractService(ShowContractService showContractService) {
		this.showContractService = showContractService;
	}

	public List getHtList() {
		return htList;
	}

	public void setHtList(List htList) {
		this.htList = htList;
	}

	public String getPage_show() {
		return page_show;
	}

	public void setPage_show(String page_show) {
		this.page_show = page_show;
	}

	public THtdelay getDelay() {
		return delay;
	}

	public void setDelay(THtdelay delay) {
		this.delay = delay;
	}

	public List getHtdelay() {
		return htdelay;
	}

	public void setHtdelay(List htdelay) {
		this.htdelay = htdelay;
	}

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	public TContract getContract() {
		return contract;
	}

	public void setContract(TContract contract) {
		this.contract = contract;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getClientname() {
		return clientname;
	}

	public void setClientname(String clientname) {
		this.clientname = clientname;
	}

	public String getCustomcode() {
		return customcode;
	}

	public void setCustomcode(String customcode) {
		this.customcode = customcode;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getModeofrepayment() {
		return modeofrepayment;
	}

	public void setModeofrepayment(String modeofrepayment) {
		this.modeofrepayment = modeofrepayment;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	public List<?> getEnsureList() {
		return ensureList;
	}

	public void setEnsureList(List<?> ensureList) {
		this.ensureList = ensureList;
	}

	public List<?> getMortgageList() {
		return mortgageList;
	}

	public void setMortgageList(List<?> mortgageList) {
		this.mortgageList = mortgageList;
	}

	public List<?> getPledgeList() {
		return pledgeList;
	}

	public void setPledgeList(List<?> pledgeList) {
		this.pledgeList = pledgeList;
	}

	public String getE() {
		return E;
	}

	public void setE(String e) {
		E = e;
	}

	public String getM() {
		return M;
	}

	public void setM(String m) {
		M = m;
	}

	public String getP() {
		return P;
	}

	public void setP(String p) {
		P = p;
	}

	public List<Loangrant> getLoangrantList() {
		return loangrantList;
	}

	public void setLoangrantList(List<Loangrant> loangrantList) {
		this.loangrantList = loangrantList;
	}

	public Double getCount() {
		return count;
	}

	public void setCount(Double count) {
		this.count = count;
	}

	public Double getUngrant() {
		return ungrant;
	}

	public void setUngrant(Double ungrant) {
		this.ungrant = ungrant;
	}

	public String getCodetype() {
		return codetype;
	}

	public void setCodetype(String codetype) {
		this.codetype = codetype;
	}
	public List<?> getContractList() {
		return contractList;
	}
	public void setContractList(List<?> contractList) {
		this.contractList = contractList;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

}
